package com.tongguan.paypos.common;

import com.tongguan.paypos.utils.FileUtils;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectStreamException;
import java.io.Serializable;

/**
 * 全局 User  序列化本地，防止内存清空，丢失崩溃
 * Created by Joylive on 2016/12/7 .
 */

public class User implements Serializable, Cloneable {

    private static final long serialVersionUID = -3631684922062966192L;

    private static final String TAG = "User";
    private static User instance;
    private String account;
    private String merchantName;
    private String merchantId;
    private String wxMerchantName;
    private String wxMerchantId;
    private String zfbMerchantName;
    private String zfbMerchantId;
    private String posId;
    private boolean loginStatus;
    private int netConnectType;

    private User() {
    }

    public static User getInstance() {
        if (instance == null) {
            Object object = FileUtils.restoreObject(AppConstants.CACHE_DIR + TAG);
            if (object == null) {
                object = new User();
                FileUtils.saveObject(AppConstants.CACHE_DIR + TAG, object);
            }
            instance = (User) object;
        }
        return instance;
    }

    public void reset() {
        account = null;
        merchantName = null;
        merchantId = null;
        wxMerchantName = null;
        wxMerchantId = null;
        zfbMerchantName = null;
        zfbMerchantId = null;
        posId = null;
        loginStatus = false;
        netConnectType = 0;
    }

    public void save() {
        FileUtils.saveObject(AppConstants.CACHE_DIR + TAG, this);
    }

    public String getAccount() {
        return account;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public String getMerchantName() {
        return merchantName;
    }

    public void setMerchantName(String merchantName) {
        this.merchantName = merchantName;
    }

    public String getMerchantId() {
        return merchantId;
    }

    public void setMerchantId(String merchantId) {
        this.merchantId = merchantId;
    }

    public String getPosId() {
        return posId;
    }

    public void setPosId(String posId) {
        this.posId = posId;
    }

    public boolean isLoginStatus() {
        return loginStatus;
    }

    public void setLoginStatus(boolean loginStatus) {
        this.loginStatus = loginStatus;
    }

    public int getNetConnectType() {
        return netConnectType;
    }

    public void setNetConnectType(int netConnectType) {
        this.netConnectType = netConnectType;
    }

    public String getWxMerchantName() {
        return wxMerchantName;
    }

    public void setWxMerchantName(String wxMerchantName) {
        this.wxMerchantName = wxMerchantName;
    }

    public String getWxMerchantId() {
        return wxMerchantId;
    }

    public void setWxMerchantId(String wxMerchantId) {
        this.wxMerchantId = wxMerchantId;
    }

    public String getZfbMerchantName() {
        return zfbMerchantName;
    }

    public void setZfbMerchantName(String zfbMerchantName) {
        this.zfbMerchantName = zfbMerchantName;
    }

    public String getZfbMerchantId() {
        return zfbMerchantId;
    }

    public void setZfbMerchantId(String zfbMerchantId) {
        this.zfbMerchantId = zfbMerchantId;
    }

    //序列化不生成新对象
    public User readResolve() throws ObjectStreamException, CloneNotSupportedException {
        instance = (User) this.clone();
        return instance;
    }

    private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
        ois.defaultReadObject();
    }

    public Object Clone() throws CloneNotSupportedException {
        return super.clone();
    }

}
